<template>
    <el-divider/>
    <el-icon>
        <DocumentChecked/>
    </el-icon>
    <span>检查结果录入</span>
    <el-divider/>
    <div class="patientSearch">
        <div>
            <el-input v-model="searchInfo.caseNumber" placeholder="请输入患者病历号"/>
        </div>
        <div>
            <el-input v-model="searchInfo.realName" placeholder="请输入患者姓名"/>
        </div>
        <div>
            <el-button @click="queryPatientByCondition">搜索</el-button>
        </div>
    </div>
    <el-table stripe table-layout="auto" :data="patientInfo">
        <el-table-column label="患者病历号">
            <template #default="scope">
                <div style="display: flex; align-items: center">
                    <span style="margin-left: 10px">{{ scope.row.caseNumber }}</span>
                </div>
            </template>
        </el-table-column>
        <el-table-column label="患者姓名">
            <template #default="scope">
                <div style="display: flex; align-items: center">
                    <span style="margin-left: 10px">{{ scope.row.realName }}</span>
                </div>
            </template>
        </el-table-column>
        <el-table-column label="患者性别">
            <template #default="scope">
                <div style="display: flex; align-items: center">
                    <span style="margin-left: 10px">{{ scope.row.gender }}</span>
                </div>
            </template>
        </el-table-column>
        <el-table-column label="患者年龄">
            <template #default="scope">
                <div style="display: flex; align-items: center">
                    <span style="margin-left: 10px">{{ scope.row.age }}</span>
                    <span style="margin-left: 10px">{{ scope.row.ageType }}</span>
                </div>
            </template>
        </el-table-column>
        <el-table-column>
            <template #default="scope">
                <el-button size="small" @click="queryPatientTech(scope.row)">已完成检查</el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-divider/>
    <el-table
            ref="singleTableRef"
            :data="techInfoList"
            style="width: 100%"
            highlight-current-row
            @current-change="handleCurrentChange"
    >
        <el-table-column property="techCode" label="检查编码" width="240"/>
        <el-table-column property="techName" label="检查名称" width="360"/>
        <el-table-column property="checkTime" label="检查时间"/>
        <el-table-column property="techPrice" label="单价"/>
        <el-table-column property="priceType" label="费用分类"/>

    </el-table>
    <div style="margin-top: 20px">
        <el-tag class="ml-2" size="large">已选择的检查项目：{{ selectTemp.techName }}</el-tag>
    </div>
    <el-divider/>
    <div>确认检查科室和检查医生</div>
    <table class="regist-info">
        <tr>
            <td>科室选择</td>
            <td>
                <el-input @click="departmentTable = true" v-model="deptmentName" placeholder="请选择科室"/>
            </td>
            <td>医生选择</td>
            <td>
                <el-select class="m-2" placeholder="请选择"
                           @change="queryDoc" v-model="empId">
                    <el-option
                            v-for="item in empList"
                            :key="item.id"
                            :label="item.realname"
                            :value="item.id"
                    />
                </el-select>
            </td>
        </tr>
    </table>
    <div>输入检查结果</div>
    <table class="regist-info">
        <tr>
            <td>检查结果</td>
            <td>
                <el-input placeholder="请输入检查结果" v-model="checkRes.checkResult"/>
            </td>
        </tr>
    </table>
    <el-drawer
            v-model="departmentTable"
            title="请选择科室"
            direction="rtl"
            size="50%"
            @open="queryAllDepartment">
        <el-table :data="departmentList" style="width: 100%">
            <el-table-column label="科室ID" prop="deptCode"/>
            <el-table-column label="科室名称" prop="deptName"/>
            <el-table-column label="科室种类" prop="deptType"/>
            <el-table-column align="right">
                <template #header>
                    <el-input v-model="search" size="small" placeholder="Type to search"/>
                </template>
                <template #default="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row);queryDoc()">
                        选择
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-drawer>
    <el-divider/>
    <el-button type="primary" @click="updateCheckResult()">结果提交</el-button>
    <el-button type="primary" @click="empId=''">重置医生</el-button>
</template>

<script setup>
import {DocumentChecked} from "@element-plus/icons-vue";
import {provide, ref} from "vue";
import axios from "axios";

axios.defaults.baseURL = "http://localhost:8092/"
provide("axios", axios)
const departmentTable = ref(false)
//患者信息
const patientInfo = ref([])
//搜索栏信息
const searchInfo = ref({
    caseNumber: '',
    realName: ''
})
//医生列表
const empList = ref([])
//患者已经执行完检查的医技项目列表
const techInfoList = ref([])
//选择检查项目临时列表
const selectTemp = ref({})
//科室列表
const departmentList = ref([])
//科室名称
const deptmentName = ref("")
const empId = ref()
//排班信息
const schedulingInfo = ref({})
//患者检查结果
const checkRes = ref({
    inputcheckEmployeeId:'',
    checkResult: '',
    checkState: '已出结果',
    id: ''

})
//医技项目列表
const techList = ref([])
//选择科室信息
function handleEdit(val) {
    deptmentName.value = departmentList.value[val].deptName
    patientInfo.value.deptmentId = departmentList.value[val].id
    //根据科室信息和排班id查询当前科室的医生
}

function queryPatientByCondition() {
    axios({
        method: 'post',
        url: 'register/queryPatientByNameOrCaseNum',
        data: searchInfo.value
    }).then(resp => {
        patientInfo.value[0] = resp.data.data
        console.log(patientInfo.value)
    })
}

function queryPatientTech(val) {
    console.log(val)
    axios({
        method: 'get',
        url: 'checkRequest/queryCheckById/' + patientInfo.value[0].id + '/执行完成'
    }).then(resp => {
        techInfoList.value = resp.data.data
        console.log(techInfoList.value)
    })
}

function handleCurrentChange(val) {
    selectTemp.value = val
    console.log(selectTemp.value)
}

//查询排班信息
function querySchedulingId() {
    let weekRule = ''
    if (new Date().getDay() == 1 || new Date().getDay() == 3 || new Date().getDay() == 5) {
        if (patientInfo.value.noon == '上午') {
            weekRule = '00100010001000'
        } else {
            weekRule = '00010001000100'
        }
    }
    if (new Date().getDay() == 2 || new Date().getDay() == 4 || new Date().getDay() == 6) {
        if (patientInfo.value.noon == '上午') {
            weekRule = '00001000100010'
        } else {
            weekRule = '00000100010001'
        }
    }
    if (new Date().getDay() == 6 || new Date().getDay() == 0) {
        weekRule = '11000000000011'
    }
    axios({
        method: 'get',
        url: 'scheduling/querySchedulingByRule/' + weekRule
    }).then(resp => {
        schedulingInfo.value = resp.data.data
    })
}

querySchedulingId()

// 查询所有科室
function queryAllDepartment() {
    axios({
        method: 'get',
        url: 'department/queryAllDepartment'
    }).then(resp => {
        departmentList.value = resp.data.data
    })
}

//查询值班医生列表
function queryDoc(val) {
    patientInfo.value.registMoney = 0
    axios({
        method: 'get',
        url: 'employee/queryDocForCheck/' + patientInfo.value.deptmentId + '/' + schedulingInfo.value.id
    }).then(resp => {
        empList.value = resp.data.data
    })
}
//根据患者id查询医技项目列表
function queryTech() {
    axios({
        method: 'get',
        url: 'checkRequest/queryCheckById/' + patientInfo.value.id + '/执行完成'
    }).then(resp => {
        techList.value = resp.data.data
        //设置默认选择的项目为第一个
        selectTemp.value = techList.value[0]
    })
}
//更新患者的检查结果
function updateCheckResult() {
//更新checkRequest表中信息,需要医生id和检查申请id
    checkRes.value.id=selectTemp.value.checkRequestId
    checkRes.value.inputcheckEmployeeId=empId.value
    console.log(checkRes.value)
    axios({
        method: 'post',
        url: 'checkRequest/patientUpdateCheckRes',
        data: checkRes.value
    }).then(resp => {
        if (resp.data.success) {
            queryPatientTech()
        }
    })
}
</script>

<style scoped>
.demo-pagination-block + .demo-pagination-block {
    margin-top: 10px;
}

.demo-pagination-block .demonstration {
    margin-bottom: 16px;
}

.patientSearch {
    display: flex;
}

.patientSearch div {
    margin: 0 1vw 0 0vw;
}


.patientNum div {
    margin: 0 1vw;
}

.regist-info {
    width: 55%;
}

.regist-info table {
    width: 100%;
    margin: 15px 0;
    border: 0;
}

.regist-info th {
    font-weight: bold;
    background-color: #c6ebfe;
    color: #2073fe
}

.regist-info, .regist-info th, .regist-info td {
    font-size: 0.95em;
    padding: 1.1vw;
    border-collapse: collapse;
}

.regist-info th {
    border: 1px solid #c6ebfe;
    border-width: 1px
}

.regist-info td {
    border: 1px solid #c6ebfe;
    border-width: 1px
}

.regist-info tr {
    border: 1px solid #ffffff;
}

.regist-info tr:nth-child(odd) {
    background-color: #f7f7f7;
}

.regist-info tr:nth-child(even) {
    background-color: #ffffff;
}
</style>